package metis.lab.hygeia.resources;

import java.io.IOException;
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.Iterator;
import org.restlet.data.Form;  
import org.restlet.data.MediaType;  
import org.restlet.data.Status;  
import org.restlet.ext.xml.DomRepresentation;  
import org.restlet.representation.Representation;  
import org.restlet.representation.StringRepresentation;  
import org.restlet.resource.Get;  
import org.restlet.resource.Post;  
import org.w3c.dom.Document;  
import org.w3c.dom.Element; 
import org.restlet.resource.ServerResource;

import metis.lab.hygeia.pojos.Account;

public class AccountResource extends ServerResource {
	private static final Logger log = Logger.getLogger(AccountResource.class.getName());
	
	 /** 
     * Handle POST requests: create a new item. 
     */  
    @Post  
    public Representation acceptItem(Representation entity) {  
        Representation result = null;  
        // Parse the given representation and retrieve pairs of  
        // "name=value" tokens.  
        log.info("Procesando POST");
        Form form = new Form(entity);  
        String itemName = form.getFirstValue("name");  

  
        // Register the new item if one is not already registered. 
        log.info(" POST: "+ itemName);
        
        boolean res=true;
        
        if (res) {  
            // Set the response's status and entity  
            setStatus(Status.SUCCESS_CREATED);  
            Representation rep = new StringRepresentation("Item created: "+ itemName,  
                    MediaType.TEXT_PLAIN);  
            // Indicates where is located the new resource.  
            rep.setLocationRef(getRequest().getResourceRef().getIdentifier()  
                    + "/" + itemName);  
            
            result = rep;  
        } else { // Item is already registered.  
            setStatus(Status.CLIENT_ERROR_NOT_FOUND);  
            result = generateErrorRepresentation("Item " + itemName  
                    + " already exists.", "1");  
        }  
  
        return result;  
    }  
  
    /** 
     * Generate an XML representation of an error response. 
     *  
     * @param errorMessage 
     *            the error message. 
     * @param errorCode 
     *            the error code. 
     */  
    private Representation generateErrorRepresentation(String errorMessage,  
            String errorCode) {  
        DomRepresentation result = null;  
        // This is an error  
        // Generate the output representation  
        try {  
            result = new DomRepresentation(MediaType.TEXT_XML);  
            // Generate a DOM document representing the list of  
            // items.  
            Document d = result.getDocument();  
  
            Element eltError = d.createElement("error");  
  
            Element eltCode = d.createElement("code");  
            eltCode.appendChild(d.createTextNode(errorCode));  
            eltError.appendChild(eltCode);  
  
            Element eltMessage = d.createElement("message");  
            eltMessage.appendChild(d.createTextNode(errorMessage));  
            eltError.appendChild(eltMessage);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        return result;  
    }  
  
    /** 
     * Returns a listing of all registered items. 
     */  
    @Get("xml")  
    public Representation toXml() {  
        // Generate the right representation according to its media type.  
        try {  
            DomRepresentation representation = new DomRepresentation(  
                    MediaType.TEXT_XML);  
  
            // Generate a DOM document representing the list of  
            // items.  
            Document d = representation.getDocument();  
            Element r = d.createElement("items");  
            d.appendChild(r);  
            ArrayList<Account> testl =testgetItems();
            Iterator<Account> it = testl.iterator();
            
            while(it.hasNext()) {
            	Account item= it.next();
                Element eltItem = d.createElement("item");  
  
                Element eltName = d.createElement("name");  
                eltName.appendChild(d.createTextNode(item.getName()));  
                eltItem.appendChild(eltName);  
  
                Element eltDescription = d.createElement("description");  
                eltDescription.appendChild(d.createTextNode(item  
                        .getEmail()));  
                eltItem.appendChild(eltDescription);  
  
                r.appendChild(eltItem);  
            }  
            d.normalizeDocument();  
  
            // Returns the XML representation of this document.  
            return representation;  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
  
        return null;  
    }
    
    public ArrayList<Account> testgetItems() {  
        ArrayList<Account> l=new ArrayList<Account>();
        Account a = new Account("carlos","email","pass");
        Account b = new Account ("brenda","mail","pwd");
        l.add(a);
        l.add(b);
    	return l;  
    }   
}
